      subroutine fcnLM(mlm,nlm,X,fvec,fjac,ldfjac,iflag)

      use globais
      implicit none

      integer :: mlm,nlm,iflag,ldfjac
      double precision :: X(nlm),fvec(mlm),fjac(mlm,nlm)
      real*4 :: xmin,zmin
      integer :: i,j,kk

      double precision DNRM2

!  variaveis auxiliares/temporarias
      complex, dimension(:), allocatable :: resid1,resid2
      double precision :: xaux,h,aux
      integer :: L

      call modelo2d(X)

      if(iflag.eq.0)then ! plota

! 	    iplot = iplot +1
! 	    write(*,*)'IPLOT:', iplot,'*****X: ',(X(i),i=1,n)
! 
! 	    xmin = -(Npml-1)*delta
! 	    zmin = 0.00
! 
! 	    call systempsimage(  c,nx,nz,delta,delta,zmin,xmin,"veC_F",'velocidade (m/s)    ','profundidade (m)    ',&
! 			      &'distancia (m)       ',wbox,Lz0/4.,Lx0/4.)
! 
! 	    call systempsimage(  c0,nx0,nz0,deltax0,deltaz0,0.00,0.00,"veC0F",'velocidade (m/s)    ','profundidade (m)    ',&
! 			      &'distancia (m)       ',wbox,Lz0/4.,Lx0/4.)
! 
! 	    call systempsimage(rho,nx,nz,delta,delta,zmin,xmin,"densi",'densidade  (kg/m3)  ','profundidade (m)    ',&
! 			      &'distancia (m)       ',wbox,Lz0/4.,Lx0/4.)


	    write(unitsol,rec=iplot)((c(i,j),j=1,Nz),i=1,Nx)



      else if(iflag.eq.1 ) then! avalia

	    call pd2d(omegas(kw),iflag)

	    do i=1,Mlm/2
	      fvec(i) = real(resid(i))
	    enddo
	    do i=1,Mlm/2
	      fvec(Mlm/2+ i) = aimag(resid(i))
	    enddo

	    write(*,*)'            X',X
	    write(*,*)'norma 2 FVEC1',DNRM2(mlm,fvec,1)

      else if(iflag.eq.2 ) then! avalia fvec e fjac  
   
	    call pd2d(omegas(kw),iflag)
	    do i=1,Mlm/2
	      fvec(i) = real(resid(i))
	    enddo
	    do i=1,Mlm/2
	      fvec(Mlm/2+ i) = aimag(resid(i))
	    enddo

	    write(*,*)'            X',X
	    write(*,*)'norma 2 FVEC2',DNRM2(mlm,fvec,1)

! !   grande suspeita de que o fjac assim esteja errado... montar J real equivalente ao J complexo
! 	    do i=1,Mlm/2
! 		do kk=1,Nlm
! 		    fjac(i,kk) = real(Jacob(i,kk))
! 		enddo
! 	    enddo
! 	    do i=1,Mlm/2
! 		do kk=1,Nlm
! 		    fjac(Mlm/2+i,kk) = aimag(conjg(Jacob(i,kk)))
! 		enddo
! 	    enddo


! ! ! gradiente por diferencas finitas
!       write(*,*)'gradiente por diferencas finitas'
! 
!       allocate(resid1(nrec))
!       allocate(resid2(nrec))
!       do j=1,N
! 	  xaux = X(j)
! 	  h = 0.01
! ! 	  do kk=1,20
! 	      X(j) = (1.+h)*xaux
! 	      call modelo2d(X,n)
! 	      call pd2d(omegas(kw),1)
! 	      resid1=resid
! 
! 	      X(j) = (1.-h)*xaux
! 	      call modelo2d(X,n)
! 	      call pd2d(omegas(kw),1)
! 	      resid2=resid
! 
! 	      do i=1,Mlm/2
! 		  fjac(i,j) = real(resid2(i)-resid1(i))/(2.*h*xaux)
! 	      enddo
! 	      do i=1,Mlm/2
! 		  fjac(Mlm/2+i,j) = -aimag(resid2(i)-resid1(i))/(2.*h*xaux)
! 	      enddo
! 
! ! 	      write(*,*)i,kk,(f1-f2)/(2.*h*xaux),f1,f2,2.*h*xaux
! ! 	      h=h*.5
! ! 	  enddo
! 
! 	  X(j) =xaux
!       enddo! loop das incognitas
!       deallocate(resid1)
!       deallocate(resid2)
! 
! ! 	    write(*,*)'fjac',fjac
      endif




! ! ! outra função de teste
!       do i=1,n
! 	  Fvec(i)=(X(i)-i*10)
!       enddo
! 
! 	fjac = 0.
!       do i=1,n
! 	fjac(i,i) = 1.
!       enddo
!        write(*,*)'X ',X,'resid ',dnrm2(n,X,1)


      return
      end
